//Copyright 2001-2004 Jake Bordens
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

//Presence Show Values
DefConst('kShowAway, "AWAY");
DefConst('kShowChat, "CHAT");
DefConst('kShowDND, "DND");
DefConst('kShowXA, "XA");


//XML Commands
DefConst('kProlog, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
DefConst('kMakeStreamXML, func (server)
	begin
		return "<stream:stream to=\"" & server &"\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"
	end);
	
DefConst('kMakeSimpleAuthXML, func(username, password, resource)
	begin
		local XMLOut := "<iq type=\"set\">";
		XMLOut := XMLOut &  "<query xmlns=\"jabber:iq:auth\">";
		XMLOut := XMLOut &  "<username>" & username & "</username>";
		XMLOut := XMLOut &  "<password>" & password & "</password>";
		XMLOut := XMLOut &  "<resource>" & resource & "</resource>";
		XMLOut := XMLOut &  "</query></iq>";
		return XMLOut
	end);

DefConst('kMakeMessageXML, func(toJID, message)
	begin
		local XMLout := "<message to=\"" & toJID & "\">";
		XMLout := XMLout & "<body>" & 
			call kEncodeEntities with (message)
				 & "</body>";
		XMLout := XMLout & "</message>";
		return XMLout
	end);

DefConst('kMakePresenceXML, func(show, status)
	begin
		local XMLout := "<presence>";
		if status then
			XMLout := XMLout & "<status>" & status & "</status>";
		if show then
			XMLout := XMLout & "<show>" & show & "</show>";
		XMLout := XMLout & "</presence>";
		return XMLout;
	end);

//parse functions	
DefConst('kParseJabberID, func(jabberID)
	begin
		local atPos := CharPos(jabberID, $@, 0);
		if atPos = nil then
			return { user: jabberID };
		
		local slashPos := CharPos(jabberID, $/, atPos);

		local userName := SubStr(jabberID,0,atpos);
		local serverName := nil;
		local resource := nil;
	
		if slashPos then
			begin
			serverName := SubStr(jabberID,atpos+1,slashPos-atPos-1);
			resource := SubStr(jabberID,slashPos+1,nil);
			end;
		else
			serverName := SubStr(jabberID,atpos+1,nil);
			
		return { user: userName, 
				 server: serverName, 
				 address: userName & "@" & servername,
				 resource: resource, };
	end);

DefConst('kParseGatewayID, func(gatewayid)
	begin
		local slashPos := CharPos(gatewayID, $/, 0);
		if not slashPos then return gatewayID;
		return SubStr(GatewayID,0,slashPos);
	end;
);